﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NjaNjam.MongoRepository;
using NjamNjam.Model.Entities;
using NjamNjamAdminApp.ImageService;
using NjamNjamAdminApp.Interfaces;
using System.Linq;

namespace NjamNjamAdminApp.Views
{
    public partial class PonudaView : DevExpress.XtraEditors.XtraForm, IView
    {
        readonly MongoRepository m_Repository = new MongoRepository();

        readonly BindingList<Ponuda> m_Ponuda = new BindingList<Ponuda>(); 

        readonly BindingList<VrstaPonude> m_VrstePonude = new BindingList<VrstaPonude>(); 
        
        public PonudaView()
        {
            InitializeComponent();
            ponudaBindingSource.DataSource = m_Ponuda;
            vrstaPonudeBindingSource.DataSource = m_VrstePonude;
            m_Ponuda.AddingNew += (s, e) =>
                                  {
                                      long id = 1;
                                      if (m_Ponuda.Count != 0)
                                      {
                                          id = m_Ponuda.Max(x => x.Id) + 1;
                                      }
                                      var ponuda = new Ponuda{Id = id};
                                      e.NewObject = ponuda;
                                  };
            gridView2.KeyDown += (s, e) =>
                                 {
                                     if (e.KeyData == Keys.Delete)
                                     {
                                         ObrisiVrstuPonude();
                                     }
                                 };
        }

        private void ObrisiVrstuPonude()
        {
            var vrstaPonude = gridView2.GetFocusedRow() as VrstaPonude;
            if (vrstaPonude != null)
            {
                IzabranaPonuda().VrstePonude.Remove(vrstaPonude);
                vrstePonudeBindingSource.ResetBindings(true);
            }
        }

        public void NoviUnos()
        {
            m_Ponuda.AddNew();
            ponudaBindingSource.MoveLast();
        }

        public void Sacuvaj()
        {
            var izabranaPonuda = IzabranaPonuda();
            if (izabranaPonuda != null)
            {
                m_Repository.Sacuvaj(izabranaPonuda);
            }
        }

        public void Obrisi()
        {
            var izabranaPonuda = IzabranaPonuda();
            if (izabranaPonuda != null)
            {
                if (gridControl2.Focused)
                {

                } else
                {
                    if (((IShell)MdiParent).ShowQuestion("Da li želite da obrišete izabranu ponudu?"))
                    {
                        m_Repository.Obrisi(izabranaPonuda);
                    }
                }
            }
        }

        public void Osvezi()
        {
            m_Ponuda.Clear();
            var ponuda = m_Repository.Ponuda();
            foreach (var p in ponuda)
            {
                m_Ponuda.Add(p);
            }
            m_VrstePonude.Clear();
            var vrstaPonude = m_Repository.VrstePonude();
            foreach (var vp in vrstaPonude)
            {
                m_VrstePonude.Add(vp);
            }
        }

        public void Izvezi()
        {
            using (var svc = new ImageServiceClient("Http"))
            {
                try
                {
                    foreach (var ponuda in m_Ponuda)
                    {
                        var response = svc.SacuvajSlikuPonude(new SaveImageRequest { Id = ponuda.Id, Image = ponuda.Slika });
                        if (response.ErrorMessage != null)
                        {
                            ((IShell)MdiParent).ShowWarning("Nesupešan izvoz slike " + ponuda.Naziv + ": " + response.ErrorMessage);
                            break;
                        }
                    }
                }
                catch (Exception exc)
                {
                    ((IShell)MdiParent).ShowWarning("Nesupešan izvoz slike: " + exc.Message);
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            Osvezi();
        }

        private Ponuda IzabranaPonuda()
        {
            return gridView1.GetFocusedRow() as Ponuda;
        }
    }
}